package br.com.ss.bean;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import br.com.ss.entity.User;
import br.com.ss.facade.UserFacade;


@SessionScoped
@ManagedBean
public class UserBean extends AbstractMenageBean {
	
	private User user;
	 
    @EJB
    private UserFacade userFacade;
 
    public User getUser(){
        if(user == null){
            ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
            String userEmail = context.getUserPrincipal().getName();
 
            user = userFacade.findUserByEmail(userEmail);
        }
 
        return user;
    }
 
    public boolean isUserAdmin(){
        return getRequest().isUserInRole("ADMIN");
    }
 
    public String logOut(){
        getRequest().getSession().invalidate();
        return "logout";
    }
 
    private HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    }
}
